import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import '../../helper/cacheHelper.dart';

import '../../model/station.dart';
import '../../widgets/someCards.dart';

class StationBookPage extends StatefulWidget {
  final bool ifAppbar;
  @override
  StationBookPage({this.ifAppbar = false});

  @override
  StationBookPageState createState() => new StationBookPageState();
}

class StationBookPageState extends State<StationBookPage> {
  late List<Station> StationList;
  final riverList = [
    "岷江",
    "黑水河",
    "杂谷脑河",
    "草坡河",
    "桃关沟",
    "渔子溪",
    "寿溪",
    "龙溪",
    "白沙河",
    "小姓河"
  ];

  @override
  initState() {
    super.initState();

    getStation();
  }

  void getStation() async {
    var _list = await CacheHelper.GetStationList();

    setState(() {
      StationList = _list;
    });
  }

  List<Widget> getAllStationList(BuildContext context) {
    var _list1 = <Widget>[];
    for (String a in riverList) {
      _list1.add(getStationList(context, a));
    }

    return _list1;
  }

  Widget getStationList(BuildContext context, String sttp) {
    var _list1 = <Widget>[];
    var _list2 = StationList.where((item) {
      return item.River == sttp;
    }).toList();
    for (Station a in _list2) {
      _list1.add(Divider(
        height: 1.0,
      ));
      _list1.add(SomeCard.StationCard2(context, a));
    }

    return Card(
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        margin: EdgeInsets.only(top: 6.0),
        child: ExpansionTile(
            leading: Icon(
              FontAwesomeIcons.leaf,
              size: 20.0,
            ),
            title: Text(sttp),
            children: _list1));
  }

  Widget getBody(BuildContext context) {
    if (StationList == null)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    // TODO: implement build
    return new SingleChildScrollView(
        // margin: EdgeInsets.only(top: 8.0),
        child: Column(children: getAllStationList(context)));
  }

  @override
  Widget build(BuildContext context) {
    //getStation();
    return Scaffold(
        appBar: widget.ifAppbar ? AppBar(title: Text('测站列表')) : null,
        body: getBody(context));
  }
}
